package cn.kgc.springcloud.service.impl;

import cn.kgc.springcloud.client.ClazzClient;
import cn.kgc.springcloud.entity.Clazz;
import cn.kgc.springcloud.entity.Group;
import cn.kgc.springcloud.mapper.GroupMapper;
import cn.kgc.springcloud.service.GroupService;
import cn.kgc.springcloud.vo.GroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/8/30
 */
@Service
public class GroupServiceImpl implements GroupService {
    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private ClazzClient clazzClient;

    @Override
    public List<GroupVO> findAll() {

        List<Group> groups =   groupMapper.selectAll();
        ArrayList<GroupVO> groupVOS = new ArrayList<>();

        groups.forEach(group -> {
            GroupVO groupVO = new GroupVO();
            BeanUtils.copyProperties(group, groupVO);
            //获取clazzId
            Integer clazzId = group.getClazzId();
            // 调用班级服务 根据id查询班级信息
            Clazz clazz = clazzClient.getById(clazzId);
            groupVO.setClazz(clazz);
            groupVOS.add(groupVO);

        });
        return groupVOS;
    }

    @Override
    public Group addGroup(Group group) {
         groupMapper.addGroup(group);
        return group;
    }

    @Override
    public Group getById(Integer groupId) {

        Group group =  groupMapper.selectById(groupId);
        return group;
    }
}
